package com.gulimall.seckill.controller;

import com.alibaba.fastjson2.JSONObject;
import com.gulimall.common.utils.R;
import com.gulimall.seckill.service.SeckillService;
import com.gulimall.seckill.vo.SeckillSkuSessionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author kkk
 * @date 2022/8/11
 */
@RestController
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @GetMapping("/seckillSku/list")
    public R getSeckillSkuList() {
        List<SeckillSkuSessionVo> seckillSkuList = seckillService.getSeckillSkuList();

        return R.ok().put("data", seckillSkuList);
    }

    @GetMapping("/sekcillSku/{skuId}")
    public R getSeckillBySkuId(@PathVariable Long skuId) {
        SeckillSkuSessionVo.SeckillSkuRelationEntity seckillSkuRelation = seckillService.getSeckillBySkuId(skuId);

        return R.ok().put("seckillSku", JSONObject.toJSONString(seckillSkuRelation));
    }

    @GetMapping("/seckill")
    public R seckillSku(@RequestParam String skuInfo, @RequestParam String key, @RequestParam Integer count) {
        String orderSn = seckillService.seckill(skuInfo, key, count);

        return R.ok().put("orderSn", orderSn);
    }
}
